package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annotation.AddCacheAnnotation;
import com.baizhi.cmfz.annotation.DeleteCahceAnnotation;
import com.baizhi.cmfz.dao.AdminDao;
import com.baizhi.cmfz.entity.Admin;
import com.baizhi.cmfz.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class AdminServiceImpl implements AdminService {

	@Autowired(required = false)
	private AdminDao adminDao;


	@Override
	@Transactional(readOnly = true)
	public Admin selectAdminByNameAndPassword(String name, String pwd) {
		Admin admin = adminDao.selectAdminByNameAndPassword(name);
		if(admin!=null){
			if (pwd.equals(admin.getAdminPassword())) return admin;
		}else{
			return null;
		}
		return admin;
	}

	@Override
	@Transactional(readOnly = true)
	@AddCacheAnnotation
	public Map selectAdminPage(int page, int limit) {
		int offset = (page-1)*limit;
		Map map = new HashMap();
		List<Admin> adminList = adminDao.selectAdminPage(offset,limit);
		int count = adminDao.selectCount();

		map.put("data",adminList);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;

	}

	@Override
	@DeleteCahceAnnotation
	public Map insertAdmin(Admin admin) {
		Map map = new HashMap();
		int i = adminDao.insertAdmin(admin);
		if(i > 0){
			map.put("isInsert","添加成功");
		}else{
			map.put("isInsert","添加失败");
		}
		return map;
	}

	@Override
	@DeleteCahceAnnotation
	public Map updateAdmin(Admin admin) {
		Map map = new HashMap();
		int i = adminDao.updateAdmin(admin);
		if(i > 0){
			map.put("isUpdate","修改成功");
		}else{
			map.put("isUpdate","修改失败");
		}
		return map;
	}

	@Override
	@DeleteCahceAnnotation
	public Map deleteAdmin(int adminId) {
		Map map = new HashMap();
		int i = adminDao.deleteAdmin(adminId);
		if(i > 0){
			map.put("isDelete","删除成功");
		}else{
			map.put("isDelete","删除失败");
		}
		return map;
	}

	@Override
	@AddCacheAnnotation
	public Admin selectById(int id) {
		return adminDao.selectById(id);
	}

	@Override
	public Set<String> selectAdminRole(String adminName) {
		return adminDao.selectAdminRole(adminName);
	}

	@Override
	public Set<String> selectAdminResource(String adminName) {
		return adminDao.selectAdminResource(adminName);
	}
}
